package sample;

import annotation.ClassXpath;
import annotation.FieldXpath;
import bean.DownloadParams;
import bean.FetchParams;
import common.Constant;
import lombok.Data;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import spider.*;
import bean.FetchParams.Flag;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by chen_ on 2017/5/2.
 */
public class BtdxSpider {

    public static Spider spider = new Spider();

    @Data
    @ClassXpath(value = "//*[@class=down]")
    public static class BtdxPageDown {
        @FieldXpath(value = "//a/@href")
        private String url;
        @FieldXpath(value = "//a/@title")
        private String title;
    }
    private static void init() {
        LinkedBlockingQueue<String> q = new LinkedBlockingQueue<>(Constant.CAPACITY);
        for (int i = 1; i < 86; i++) {
            q.offer("http://www.btdx8.com/douban_rating_8/page/" + i);
        }
        spider.setQueues0(q);
    }

    public static void main(String[] args) {
        init();
        spider.chain(
                url -> FetchParams.builder().url("" + url).xpath("//*[@class=entry-title]/a/@href").flag(Flag.simple).build()
        ).chain(
                url -> FetchParams.builder().url("" + url).xpath("//*[@id='zdownload']/a/@href").flag(Flag.dynamic).build()
        ).chain(
                url -> FetchParams.builder().url("" + url).clazz(BtdxPageDown.class).flag(Flag.dynamic).script("call_down()").scriptLocator(ExpectedConditions.visibilityOfElementLocated(By.id("downlink"))).timeout(4).build()
        ).download(
                btdxPageDown -> {
                    String url = ((BtdxPageDown) btdxPageDown).getUrl();
                    String filename = "./豆瓣八分/" + ((BtdxPageDown) btdxPageDown).getTitle().split("点击下载：")[1] + ".torrent";
                    return DownloadParams.builder().url(url).filename(filename).build();
                }
        );
    }
}